package com.ibeetl.admin.core.util.beetl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ibeetl.admin.core.entity.CoreConfig;
import com.ibeetl.admin.core.service.CoreConfigService;

/**
 * 取出所有的coreConfig对象,可以通过title获取某个CoreConfig对象
 * 
 * 如果第一个参数不为空，那么title值为第一个参数的值作为查找条件，返回查到的CoreConfig对象
 */
@Component
public class ConfigFunction implements Function {

	@Autowired
	CoreConfigService configService;
	@Autowired SQLManager sqlManager;
	
	public Object call(Object[] paras, Context ctx) {
		if (paras.length == 1) {
			CoreConfig config = this.configService.getConfigByTitle((String)paras[0]);
			return config;
		}
		
		LambdaQuery<CoreConfig> query = sqlManager.lambdaQuery(CoreConfig.class);
		List<CoreConfig> list = query.select();
		Map<String,CoreConfig> configMap = new HashMap<String, CoreConfig>();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			CoreConfig coreConfig = (CoreConfig) iterator.next();
			configMap.put(coreConfig.getTitle(), coreConfig);
		}
		
		return configMap;
	}
	
	

}
